#include "Ast.h"
#include "STEClasses.h"
#include "ParserUtil.h"
/* class added for event matcher */

class StateTran {
    string srcState_;
    string dstState_;
    char c_;
    public:
    StateTran(string srcState,char c,string dstState) {
        srcState_=srcState;
        c_=c;
        dstState_=dstState;

    }
    void printStates();
    string getSrcState() { return srcState_;}
    string getDstState() { return dstState_;}
    char getC() { return c_;}
};
class State {
    private:
    string regex_;
    int num_;
    public:
    State(string regex,int num) {
        regex_=regex;
        num_ = num;
    }
};
class EventMatcher;
class EventPat {
        private:
        string regex_;
        string event_names_;
        string event_label_;
        vector<string> states_;
        vector<StateTran*> state_tran_;
        vector<string> final_states_;
        int state_num;
        public:
        EventPat(string regex,string event_names, string label) {
            regex_ = regex;
            event_names_ = event_names;
            event_label_ = "call rule " + label;
            state_num = 0;
        }

        string getRegEx() { return regex_;}
        string getEventNames() { return event_names_; }
        string getAction() {return event_label_;}
        void addState(string regex);
        vector<string> getStates() { return states_; }
        void addFinalState(string regex);
        vector<string> getFinalStates() { return final_states_; }
        void addStateTran(string srcState,char c,string dstState);
        vector<StateTran*> getStateTran() { return state_tran_;}
        bool isStatePresent(string regex);
        void printStates();
        void printStateTran();
        void createDFAForEventPat(string regex);// main function for creating DFA
         string dtranForExpr(string p, char a,int *final);// main function for calculating derivate D(P,a)
          bool eps(string p);
          string concat(string r1,string r2);
           bool checkForParenthesis(string p);
        int getExprWithParenthesis(string p);
        bool isFinalState(string p);
        int stringToInt(string p);


};
class EventMatcher {
        private:
        GlobalEntry *ge_;
        string event_names_;
        string event_regex_;
        vector<EventPat*> event_pat_list_;
        EventPat* final_pat_;
        int label;
        public :
        EventMatcher(GlobalEntry *ge) {ge_=ge;};
        bool getEventPatFromAST();
        string getRegEx() { return event_regex_;}
        string getEventNames() { return event_names_; }
        void printStateTranWithAction();
        string getActionsForState(string regex);
        void startForEventPat();
        string getNextLabel() { return itoa(++label); }

//        void testDerivative();
//        int getStateFromREs(vector<BasePatNode*>*, vector<int>* invoked_list=NULL);
//        int stateCounter;
//        vector<vector<string*>*>* rulesToStateTable;
//        vector<vector<BasePatNode*>*>* rulesToStateStorage;
//        vector<vector<int>*>* actionInvoke;
//        vector<BasePatNode*>* derivativeOfSetOfBasePat(vector<BasePatNode*>*, string, vector<int>* invoked_list=NULL);
        vector<BasePatNode*>* confineList(vector<BasePatNode*>* bp);
        vector<vector<BasePatNode*>*>* confineLists(vector<vector<BasePatNode*>*>*);


        int getStateFromAllRuleSet(vector<vector<BasePatNode*>*>* res);
        bool sameAllRulesSetSignature(vector<vector<string>*>* sign1, vector<vector<string>*>* sign2);
        bool sameRuleSetSignature(vector<string>* sign1, vector<string>* sign2);
        vector<BasePatNode*>* doTransition1Rule(vector<BasePatNode*>* bps, string eventName, int ruleNo);
        void itemSetBasedApproach(bool emitCodeOnly=false);
        vector<vector<BasePatNode*>*>* doTransitionAllRule(vector<vector<BasePatNode*>*>* bps, string eventName);
        vector<vector<string>*>* getAllRuleSetSignature(vector<vector<BasePatNode*>*>* bp);
        void printAllRuleSetSignature(vector<vector<BasePatNode*>*>* bp);
        vector<string>* get1RuleSetSignature(vector<BasePatNode*>* bp);

		vector<vector<vector<BasePatNode*>*>*>* rulesToStateMapping;
		vector<vector<vector<string>*>*>* signaturesToStateMapping;

		//return a list rule which matched the current state
		vector<int>* ruleMatchedAtState(int state);

		vector<BasePatNode*>* modifiedRuleSet;

		bool aRuleHasEPS(vector<BasePatNode*>* aRule);

        //static int stringToInt(string regex);

};


